<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

include_once(ROOT_PATH."configs/ver.php");
$SYSVER = VERSION.implode("",explode(".",BUILD));

$GLOBALDEF_LIST = array();

$STATUS_LIST = array();
$FILE_STATUS_LIST = array();
$FILE_UPLOAD_LIST = array();
$GROUP_LIST = array();
$DATE_FORMAT = array();
$DEF_TRANS = array();
$TIMEZONE_LIST = array();
$PREF_LIST = array();

if (check("GLOBALDEF_LIST")) {
	$_log->debug("Using cache definitions");
	$GLOBALDEF_LIST = retrieve("GLOBALDEF_LIST");
	foreach ($GLOBALDEF_LIST as $k => $v) {
		eval("define('{$k}', '{$v}');");
	}
	$STATUS_LIST = retrieve("STATUS_LIST");
	$FILE_STATUS_LIST = retrieve("FILE_STATUS_LIST");
	$FILE_UPLOAD_LIST = retrieve("FILE_UPLOAD_LIST");
	$GROUP_LIST = retrieve("GROUP_LIST");
	$DATE_FORMAT = retrieve("DATE_FORMAT");
	$DEF_TRANS = retrieve("DEF_TRANS");
	$TIMEZONE_LIST = retrieve("TIMEZONE_LIST");
	$PREF_LIST = retrieve("PREF_LIST");	
} else {
	$_log->debug("Cache definitions not set");
	$defs = simplexml_load_file(ROOT_PATH."configs/definitions.xml");
	foreach($defs->definition as $def) {
		$name = String::toString($def->name);
		$value = String::toString($def->value);
		$GLOBALDEF_LIST[$name]=$value;
	   eval("define('{$name}', '{$value}');");
	   if ($def->text!="") {
			$DEF_TRANS[$value]=String::toString($def->text);
	   }
	   //if (substr_count($def->name,'FILE_STATUS_')>0) {
	   if (String::startWith($def->name,"FILE_STATUS_")) {	
	   		array_push($FILE_STATUS_LIST,array("id"=>$name,"value"=>$value));
	   //} elseif (substr_count($def->name,'FILE_UPLOAD_')>0) {
		} elseif (String::startWith($def->name,"FILE_UPLOAD_")) {	   
	   		array_push($FILE_UPLOAD_LIST,array("id"=>$name,"value"=>$value));
	   //} elseif (substr_count($def->name,'STATUS_')>0) {
	   } elseif (String::startWith($def->name,"STATUS_")) {
	   		array_push($STATUS_LIST,$value);
	   //} elseif (substr_count($def->name,'GROUP_')>0) {
	   } elseif (String::startWith($def->name,"GROUP_")) {
	   		array_push($GROUP_LIST,$value);
	   }
	}
	
	$defs = simplexml_load_file(ROOT_PATH."configs/tables.xml");
	foreach($defs->table as $def) {
		$name = String::toString($def->name);
		$value = String::toString($def->value);		
		$GLOBALDEF_LIST[$name]=$value;
	   eval("define('{$name}', '{$value}');");
	}
	
	$defs = simplexml_load_file(ROOT_PATH."configs/timezones.xml");
	foreach($defs->timezone as $def) {
		$name = String::toString($def->name);
		$value = String::toString($def->value);		
		array_push($TIMEZONE_LIST,array("value" => $value,"name" => $name));
	}
	
	$defs = simplexml_load_file(ROOT_PATH."configs/dtformat.xml");
	foreach($defs->format as $def) {
		array_push($DATE_FORMAT,array("value" => String::toString($def->value)));
	}
	
	$defs = simplexml_load_file(ROOT_PATH."configs/options.xml");
	foreach($defs->option as $def) {
		$grp = "*";
		$type = "bool";
		if (isset($def->group)) {
			$grp = util::stringToArray(String::toString($def->group),",");
		}
		if (isset($def->type)) {
			$type = String::toString($def->type);
		}
		$default = ($type=="bool" ? "false" : "");
		if (isset($def->default)) {
			$default = String::toString($def->default);
		}
		$ar = array("code" => String::toString($def->code),"text" => String::toString($def->text),"group"=>$grp, "type"=>$type, "default" => $default);
		$PREF_LIST[String::toString($def->code)]= $ar;
	}

	cache("GLOBALDEF_LIST",$GLOBALDEF_LIST);
	cache("STATUS_LIST",$STATUS_LIST);
	cache("FILE_STATUS_LIST",$FILE_STATUS_LIST);
	cache("FILE_UPLOAD_LIST",$FILE_UPLOAD_LIST);
	cache("GROUP_LIST",$GROUP_LIST);
	cache("DATE_FORMAT",$DATE_FORMAT);
	cache("DEF_TRANS",$DEF_TRANS);
	cache("TIMEZONE_LIST",$TIMEZONE_LIST);
	cache("PREF_LIST",$PREF_LIST);	
}

?>